<script>

function saveDatas()
{
	//check if ranges are distincts
	/*
	var i = 0;
	var exist = true;
	while (exist)
	{
		var from;
		var to;
		from = document.getElementById('config[' + i + '][from]');
		to = document.getElementById('config[' + i + '][to]');
		
		if (from != null)
		{
			//check from vs to
			if (parseInt(from.value) >= parseInt(to.value))
			{
				alert('<?php echo $this->__('From value must be lower than to value'); ?>');
				exist = false;
				return 0;
			}
			
			//check from vs previous to
			if (i > 0)
			{
				var previousTo;
				previousTo = document.getElementById('config[' + (i - 1) + '][to]');
				if (parseInt(from.value) <= parseInt(previousTo.value))
				{
					alert('<?php echo $this->__('Ranges must be distinct'); ?>');
					return 0;
				}
			}
		}
		else
			exist = false;
		i += 1;
	}
	*/
	
	document.getElementById('form_delays').submit();
}

</script>

<div class="content-header">
    <table cellspacing="0" class="grid-header">
        <tr>
            <td><h3><?php echo $this->__('Product availability ranges')?></h3></td>
	       <td align="right">
	            <button onclick="document.location.href='<?php echo $this->getNewRangeUrl(); ?>'" class="scalable" type="button"><span><?php echo $this->__('Add Range')?></span></button>
	            <button onclick="saveDatas();" class="scalable save" type="button"><span><?php echo $this->__('Save')?></span></button>
	        </td>
        </tr>

    </table>
</div>

<form id="form_delays" name="form_delays" method="POST" action="<?php echo $this->getUrl('SalesOrderPlanning/ProductAvailabilityRange/Save') ?>">
<input name="form_key" type="hidden" value="<?php echo $this->getFormKey() ?>" />
<div class="grid">
	<table cellspacing="0" class="data" width="100%">
		<thead>
			<tr class="headings">
				<th class="a-center"><?php echo $this->__('From')?></th>
				<th class="a-center"><?php echo $this->__('To')?></th>
				<th class="a-center"><?php echo $this->__('Default Label')?></th>
				<?php foreach($this->getStoreViewList() as $storeView): ?>
					<th class="a-center"><?php echo $storeView->getName(); ?></th>
				<?php endforeach; ?>
				<th class="a-center"><?php echo $this->__('Delete')?></th>
			</tr>
		</thead>
		<tbody>

			<?php $ranges = $this->getRanges();  ?>
			<?php for($i=0;$i<count($ranges);$i++): ?>
				<tr>
					<td class="a-center"><input type="text" size="3" id="config[<?php echo $i; ?>][from]" name="config[<?php echo $i; ?>][from]" value="<?php echo $ranges[$i]['from']; ?>"> <?php echo $this->__('days'); ?></td>
					<td class="a-center"><input type="text" size="3" id="config[<?php echo $i; ?>][to]" name="config[<?php echo $i; ?>][to]" value="<?php echo $ranges[$i]['to']; ?>"> <?php echo $this->__('days'); ?></td>
					<td class="a-center"><input type="text" size="20" id="config[<?php echo $i; ?>][label]" name="config[<?php echo $i; ?>][label]" value="<?php echo $ranges[$i]['label']; ?>"></td>
					<?php foreach($this->getStoreViewList() as $storeView): ?>
						<td class="a-center"><input type="text" size="20" id="config[<?php echo $i; ?>][<?php echo $storeView->getId(); ?>]" name="config[<?php echo $i; ?>][<?php echo $storeView->getCode(); ?>]" value="<?php if (isset($ranges[$i][$storeView->getCode()])) { echo $ranges[$i][$storeView->getCode()];} ?>"></td>
					<?php endforeach; ?>
					<td class="a-center"><input type="checkbox" value="1" id="config[<?php echo $i; ?>][delete]" name="config[<?php echo $i; ?>][delete]"></td>
				</tr>
			<?php endfor; ?>
		</tbody>
	</table>
</div>
</form>
